package com.nice.client;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;

import sun.misc.BASE64Encoder;

public class ImagUtil {

	public static BASE64Encoder encoder = new BASE64Encoder();

	public static String encoeBase64(String file, String type) {
		try {
			BufferedImage img = ImageIO.read(new File(file));
			ByteOutputStream output = new ByteOutputStream();
			ImageIO.write(img, type, output);
			byte[] bytes = output.getBytes();
			String encode = encoder.encode(bytes);
			System.out.println(bytes.length + "-encode-" + encode.length());
			return encode;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "";
	}

	public static byte[] readImg(String file) {
		try {
			FileInputStream fis = new FileInputStream(new File(file));
			byte[] buf = new byte[fis.available()];
			fis.read(buf);
			fis.close();
			return buf;
		} catch (Exception e) {
			e.printStackTrace();
			return new byte[0];
		}
	}

	public static byte[] decodeBase64(String base64Str) {
		return null;
	}

	public static void main(String[] args) {
		System.out.println(readImg("./img/test.png").length);
		System.out.println(encoeBase64("./img/test.png", "png"));
	}
}
